package control
{
	import VO.ComponentVO;
	
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.utils.*;
	
	import mx.controls.Alert;
	
	import resource.ResourceMap;
	import resource.ResourceUserInfo;
	import resource.structure.*;
	
	//主要的控制模块
	public class ControlMain
	{
		//显示窗口大小
		private var displayWidth:int;
		private var displayHeight:int;
		//每个格子的大小
		private const map_blocksize:int = 54;
		
		//等级上限
		private var levelLimit:int;
		//用户配置信息
		public var userInfo:ResourceUserInfo = new ResourceUserInfo;
		//大地图
		public var map:ResourceMap = new ResourceMap;
		//城堡
		public var castle:ResourceCastle = new ResourceCastle;
		//兵营
		public var barrack:ResourceBarrack = new ResourceBarrack;
		//农场
		public var farm:ResourceFarm = new ResourceFarm;
		//市场
		public var market:ResourceMarket = new ResourceMarket;
		//物品仓库
		public var warehouse:ResourceWarehouse = new ResourceWarehouse;
		//矿山
		public var mine:ResourceMine = new ResourceMine;
		//池塘
		public var pond:ResourcePond = new ResourcePond;
		//树
		public var tree:ResourceTree = new ResourceTree;
		//假山
		public var rockery:ResourceRockery = new ResourceRockery;
		//道路
		public var road:ResourceRoad = new ResourceRoad;
		//移动类
		public var move:ControlMove = new ControlMove;
		public function ControlMain(mode:String)
		{
			if(mode == "READ_FILE")
			{
				var request:URLRequest = new URLRequest("assets/userinfo.xml");
				var loader:URLLoader = new URLLoader();			
				loader.addEventListener(Event.COMPLETE,getUserInfo);
				loader.load(request);
			}
			else if(mode == "NOTHING")
			{
				
			}
		}
		
		//读取用户配置文件
		private function getUserInfo(event:Event):void
		{
			//var userinfonum:int = 0;
			try
			{				
				var userXML:XML = new XML(event.target.data);
				var temp:int = userXML.elements().length();
				trace(temp);
				for(var i:int = 0;i < temp;i++)
				{
					if(userXML.node[i].@type == "username")
					{
						userInfo.username = userXML.node[i].@value;
					}
					else if(userXML.node[i].@type == "map")
					{
						userInfo.mapnumber = int(userXML.node[i].@value);						
					}
					else if(userXML.node[i].@type == "castle")
					{
						userInfo.castlelevel = int(userXML.node[i].@value);						
					}
					else if(userXML.node[i].@type == "barrack")
					{
						userInfo.barracklevel = int(userXML.node[i].@value);						
					}
					else if(userXML.node[i].@type == "farm")
					{
						userInfo.farmlevel = int(userXML.node[i].@value);						
					}
					else if(userXML.node[i].@type == "market")
					{
						userInfo.marketlevel = int(userXML.node[i].@value);						
					}
					else if(userXML.node[i].@type == "mine")
					{
						userInfo.minelevel = int(userXML.node[i].@value);						
					}
					else if(userXML.node[i].@type == "warehouse")
					{
						userInfo.warehouselevel = int(userXML.node[i].@value);						
					}
					else if(userXML.node[i].@type == "pond")
					{
						userInfo.pondamount = int(userXML.node[i].@amount);						
					}
					else if(userXML.node[i].@type == "tree")
					{
						userInfo.treeamount = int(userXML.node[i].@amount);						
					}
					else if(userXML.node[i].@type == "rockery")
					{
						userInfo.rockeryamount = int(userXML.node[i].@amount);						
					}
					else if(userXML.node[i].@type == "resources")
					{
						//读取资源信息， 并存到用户model中
						var userResouce:XML = userXML.node[i];
						for(var j:int = 0; j < userResouce.elements().length(); j++)
						{
							var k:int = 0;
							if(userResouce.node[j].@type == "money")
							{
								userInfo.userResource.money = int(userResouce.node[j].@value);
							}
							else if(userResouce.node[j].@type == "ore")
							{
								var oreResource:XML = userResouce.node[j];
								for(k = 0; k < oreResource.elements().length(); k++)
								{
									userInfo.userResource.resource_ore[(int(oreResource.node[k].@level)-1)][0] = oreResource.node[k].@type;
									userInfo.userResource.resource_ore[(int(oreResource.node[k].@level)-1)][1] = int(oreResource.node[k].@num);
								}
							}
							else if(userResouce.node[j].@type == "senior ore")
							{
								var seniorOreResource:XML = userResouce.node[j];
								for(k = 0; k < seniorOreResource.elements().length(); k++)
								{
									userInfo.userResource.resource_senior_ore[(int(seniorOreResource.node[k].@level)-1)][0] = seniorOreResource.node[k].@type;
									userInfo.userResource.resource_senior_ore[(int(seniorOreResource.node[k].@level)-1)][1] = int(seniorOreResource.node[k].@num);
								}
							}
							else if(userResouce.node[j].@type == "food")
							{
								var foodResource:XML = userResouce.node[j];
								for(k = 0; k < foodResource.elements().length(); k++)
								{
									userInfo.userResource.resource_food[(int(foodResource.node[k].@level)-1)][0] = foodResource.node[k].@type;
									userInfo.userResource.resource_food[(int(foodResource.node[k].@level)-1)][1] = int(foodResource.node[k].@num);
								}
							}
							else if(userResouce.node[j].@type == "senior food")
							{
								var seniorFoodResource:XML = userResouce.node[j];
								for(k = 0; k < seniorFoodResource.elements().length(); k++)
								{
									userInfo.userResource.resource_senior_food[(int(seniorFoodResource.node[k].@level)-1)][0] = seniorFoodResource.node[k].@type;
									userInfo.userResource.resource_senior_food[(int(seniorFoodResource.node[k].@level)-1)][1] = int(seniorFoodResource.node[k].@num);
								}
							}
						}
					}
					
				}
			}
			catch(e:TypeError)
			{
				Alert.show("Can't read userinfo correctly!");
				trace(e.message);
			}
			
			//载入地图配置
			var request2:URLRequest = new URLRequest("assets/mapinfo.xml");
			var loader2:URLLoader = new URLLoader();			
			loader2.addEventListener(Event.COMPLETE,getMapInfo);
			loader2.load(request2);
		}
		
		//读取地图配置文件
		private function getMapInfo(event:Event):void
		{			
			var mapinfonum:int = 0;
			try
			{				
				var mapXML:XML = new XML(event.target.data);
				
				for(var i:int = 0;i < mapXML.elements().length();i++)
				{					
					if(mapXML.node[i].@type == "map" && int(mapXML.node[i].@id) == userInfo.mapnumber)
					{
						map.mapIma.width = int(mapXML.node[i].@width);
						map.mapIma.height = int(mapXML.node[i].@height);
						map.mapIma.background = mapXML.node[i].@background;						
						mapinfonum++;
					}
					if(mapXML.node[i].@type == "castle" && int(mapXML.node[i].@id) == userInfo.mapnumber && mapXML.node[i].@level == "01")
					{
						castle.xPosrelative = int(mapXML.node[i].@x);
						castle.yPosrelative = int(mapXML.node[i].@y);
						castle.structure.width = int(mapXML.node[i].@width);
						castle.structure.height = int(mapXML.node[i].@height);
						castle.structure.background = mapXML.node[i].@background;
						mapinfonum++;
					}
					if(mapXML.node[i].@type == "barrack" && int(mapXML.node[i].@id) == userInfo.mapnumber && mapXML.node[i].@level == "01")
					{
						barrack.xPosrelative = int(mapXML.node[i].@x);
						barrack.yPosrelative = int(mapXML.node[i].@y);
						barrack.structure.width = int(mapXML.node[i].@width);
						barrack.structure.height = int(mapXML.node[i].@height);
						barrack.structure.background = mapXML.node[i].@background;
						mapinfonum++;
					}
					if(mapXML.node[i].@type == "farm" && int(mapXML.node[i].@id) == userInfo.mapnumber && mapXML.node[i].@level == "01")
					{
						farm.xPosrelative = int(mapXML.node[i].@x);
						farm.yPosrelative = int(mapXML.node[i].@y);
						farm.structure.width = int(mapXML.node[i].@width);
						farm.structure.height = int(mapXML.node[i].@height);
						farm.structure.background = mapXML.node[i].@background;
						mapinfonum++;
					}
					if(mapXML.node[i].@type == "market" && int(mapXML.node[i].@id) == userInfo.mapnumber && mapXML.node[i].@level == "01")
					{
						market.xPosrelative = int(mapXML.node[i].@x);
						market.yPosrelative = int(mapXML.node[i].@y);
						market.structure.width = int(mapXML.node[i].@width);
						market.structure.height = int(mapXML.node[i].@height);
						market.structure.background = mapXML.node[i].@background;
						mapinfonum++;
					}
					if(mapXML.node[i].@type == "warehouse" && int(mapXML.node[i].@id) == userInfo.mapnumber && mapXML.node[i].@level == "01")
					{
						warehouse.xPosrelative = int(mapXML.node[i].@x);
						warehouse.yPosrelative = int(mapXML.node[i].@y);
						warehouse.structure.width = int(mapXML.node[i].@width);
						warehouse.structure.height = int(mapXML.node[i].@height);
						warehouse.structure.background = mapXML.node[i].@background;
						mapinfonum++;
					}
					if(mapXML.node[i].@type == "mine" && int(mapXML.node[i].@id) == userInfo.mapnumber && mapXML.node[i].@level == "01")
					{
						mine.xPosrelative = int(mapXML.node[i].@x);
						mine.yPosrelative = int(mapXML.node[i].@y);
						mine.structure.width = int(mapXML.node[i].@width);
						mine.structure.height = int(mapXML.node[i].@height);
						mine.structure.background = mapXML.node[i].@background;
						mapinfonum++;
						
					}
					if(mapXML.node[i].@type == "pond" && int(mapXML.node[i].@id) == userInfo.mapnumber)
					{
						pond.pondamount = mapXML.node[i].@amount;
						pond.structure.background = mapXML.node[i].@background;
						pond.pond.push([int(mapXML.node[i].@x),int(mapXML.node[i].@y),int(mapXML.node[i].@width),int(mapXML.node[i].@height),mapXML.node[i].@background]);
						mapinfonum++;
						
					}
					if(mapXML.node[i].@type == "road" && int(mapXML.node[i].@id) == userInfo.mapnumber && mapXML.node[i].@number == "01")
					{
						var blocknum:int = 0;
						road.roadlength1 = int(mapXML.node[i].@length);
						road.roadsize = int(mapXML.node[i].@size);
						road.roadbackground = mapXML.node[i].@background;
						if(blocknum < road.roadlength1)
						{
							road.road1.push([int(mapXML.node[i].@b00x),int(mapXML.node[i].@b00y)]);
							move.startposition.push([int(mapXML.node[i].@b00x),int(mapXML.node[i].@b00y)]);
							move.currentposition.push([int(mapXML.node[i].@b00x),int(mapXML.node[i].@b00y)]);
							blocknum++;
						}
						if(blocknum < road.roadlength1)
						{
							road.road1.push([int(mapXML.node[i].@b01x),int(mapXML.node[i].@b01y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength1)
						{
							road.road1.push([int(mapXML.node[i].@b02x),int(mapXML.node[i].@b02y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength1)
						{
							road.road1.push([int(mapXML.node[i].@b03x),int(mapXML.node[i].@b03y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength1)
						{
							road.road1.push([int(mapXML.node[i].@b04x),int(mapXML.node[i].@b04y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength1)
						{
							road.road1.push([int(mapXML.node[i].@b05x),int(mapXML.node[i].@b05y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength1)
						{
							road.road1.push([int(mapXML.node[i].@b06x),int(mapXML.node[i].@b06y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength1)
						{
							road.road1.push([int(mapXML.node[i].@b07x),int(mapXML.node[i].@b07y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength1)
						{
							road.road1.push([int(mapXML.node[i].@b08x),int(mapXML.node[i].@b08y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength1)
						{
							road.road1.push([int(mapXML.node[i].@b09x),int(mapXML.node[i].@b09y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength1)
						{
							road.road1.push([int(mapXML.node[i].@b10x),int(mapXML.node[i].@b10y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength1)
						{
							road.road1.push([int(mapXML.node[i].@b11x),int(mapXML.node[i].@b11y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength1)
						{
							road.road1.push([int(mapXML.node[i].@b12x),int(mapXML.node[i].@b12y)]);
							blocknum++;
						}
						if(blocknum < road.roadlength1)
						{
							road.road1.push([int(mapXML.node[i].@b13x),int(mapXML.node[i].@b13y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength1)
						{
							road.road1.push([int(mapXML.node[i].@b14x),int(mapXML.node[i].@b14y)]);
							blocknum++;
						}						
						mapinfonum++;						
					}
					if(mapXML.node[i].@type == "road" && int(mapXML.node[i].@id) == userInfo.mapnumber && mapXML.node[i].@number == "02")
					{
						blocknum = 0;
						road.roadlength2 = int(mapXML.node[i].@length);
						road.roadsize = int(mapXML.node[i].@size);
						road.roadbackground = mapXML.node[i].@background;
						if(blocknum < road.roadlength2)
						{
							road.road2.push([int(mapXML.node[i].@b00x),int(mapXML.node[i].@b00y)]);
							move.startposition.push([int(mapXML.node[i].@b00x),int(mapXML.node[i].@b00y)]);
							move.currentposition.push([int(mapXML.node[i].@b00x),int(mapXML.node[i].@b00y)]);
							blocknum++;
						}
						if(blocknum < road.roadlength2)
						{
							road.road2.push([int(mapXML.node[i].@b01x),int(mapXML.node[i].@b01y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength2)
						{
							road.road2.push([int(mapXML.node[i].@b02x),int(mapXML.node[i].@b02y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength2)
						{
							road.road2.push([int(mapXML.node[i].@b03x),int(mapXML.node[i].@b03y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength2)
						{
							road.road2.push([int(mapXML.node[i].@b04x),int(mapXML.node[i].@b04y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength2)
						{
							road.road2.push([int(mapXML.node[i].@b05x),int(mapXML.node[i].@b05y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength2)
						{
							road.road2.push([int(mapXML.node[i].@b06x),int(mapXML.node[i].@b06y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength2)
						{
							road.road2.push([int(mapXML.node[i].@b07x),int(mapXML.node[i].@b07y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength2)
						{
							road.road2.push([int(mapXML.node[i].@b08x),int(mapXML.node[i].@b08y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength2)
						{
							road.road2.push([int(mapXML.node[i].@b09x),int(mapXML.node[i].@b09y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength2)
						{
							road.road2.push([int(mapXML.node[i].@b10x),int(mapXML.node[i].@b10y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength2)
						{
							road.road2.push([int(mapXML.node[i].@b11x),int(mapXML.node[i].@b11y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength2)
						{
							road.road2.push([int(mapXML.node[i].@b12x),int(mapXML.node[i].@b12y)]);
							blocknum++;
						}
						if(blocknum < road.roadlength2)
						{
							road.road2.push([int(mapXML.node[i].@b13x),int(mapXML.node[i].@b13y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength2)
						{
							road.road2.push([int(mapXML.node[i].@b14x),int(mapXML.node[i].@b14y)]);
							blocknum++;
						}						
						mapinfonum++;						
					}
					if(mapXML.node[i].@type == "road" && int(mapXML.node[i].@id) == userInfo.mapnumber && mapXML.node[i].@number == "03")
					{
						blocknum = 0;
						road.roadlength3 = int(mapXML.node[i].@length);
						road.roadsize = int(mapXML.node[i].@size);
						road.roadbackground = mapXML.node[i].@background;
						if(blocknum < road.roadlength3)
						{
							road.road3.push([int(mapXML.node[i].@b00x),int(mapXML.node[i].@b00y)]);
							move.startposition.push([int(mapXML.node[i].@b00x),int(mapXML.node[i].@b00y)]);
							move.currentposition.push([int(mapXML.node[i].@b00x),int(mapXML.node[i].@b00y)]);
							blocknum++;
						}
						if(blocknum < road.roadlength3)
						{
							road.road3.push([int(mapXML.node[i].@b01x),int(mapXML.node[i].@b01y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength3)
						{
							road.road3.push([int(mapXML.node[i].@b02x),int(mapXML.node[i].@b02y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength3)
						{
							road.road3.push([int(mapXML.node[i].@b03x),int(mapXML.node[i].@b03y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength3)
						{
							road.road3.push([int(mapXML.node[i].@b04x),int(mapXML.node[i].@b04y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength3)
						{
							road.road3.push([int(mapXML.node[i].@b05x),int(mapXML.node[i].@b05y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength3)
						{
							road.road3.push([int(mapXML.node[i].@b06x),int(mapXML.node[i].@b06y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength3)
						{
							road.road3.push([int(mapXML.node[i].@b07x),int(mapXML.node[i].@b07y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength3)
						{
							road.road3.push([int(mapXML.node[i].@b08x),int(mapXML.node[i].@b08y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength3)
						{
							road.road3.push([int(mapXML.node[i].@b09x),int(mapXML.node[i].@b09y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength3)
						{
							road.road3.push([int(mapXML.node[i].@b10x),int(mapXML.node[i].@b10y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength3)
						{
							road.road3.push([int(mapXML.node[i].@b11x),int(mapXML.node[i].@b11y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength3)
						{
							road.road3.push([int(mapXML.node[i].@b12x),int(mapXML.node[i].@b12y)]);
							blocknum++;
						}
						if(blocknum < road.roadlength3)
						{
							road.road3.push([int(mapXML.node[i].@b13x),int(mapXML.node[i].@b13y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength3)
						{
							road.road3.push([int(mapXML.node[i].@b14x),int(mapXML.node[i].@b14y)]);
							blocknum++;
						}						
						mapinfonum++;						
					}
					if(mapXML.node[i].@type == "road" && int(mapXML.node[i].@id) == userInfo.mapnumber && mapXML.node[i].@number == "04")
					{
						blocknum = 0;
						road.roadlength4 = int(mapXML.node[i].@length);
						road.roadsize = int(mapXML.node[i].@size);
						road.roadbackground = mapXML.node[i].@background;
						if(blocknum < road.roadlength4)
						{
							road.road4.push([int(mapXML.node[i].@b00x),int(mapXML.node[i].@b00y)]);
							move.startposition.push([int(mapXML.node[i].@b00x),int(mapXML.node[i].@b00y)]);
							move.currentposition.push([int(mapXML.node[i].@b00x),int(mapXML.node[i].@b00y)]);
							blocknum++;
						}
						if(blocknum < road.roadlength4)
						{
							road.road4.push([int(mapXML.node[i].@b01x),int(mapXML.node[i].@b01y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength4)
						{
							road.road4.push([int(mapXML.node[i].@b02x),int(mapXML.node[i].@b02y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength4)
						{
							road.road4.push([int(mapXML.node[i].@b03x),int(mapXML.node[i].@b03y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength4)
						{
							road.road4.push([int(mapXML.node[i].@b04x),int(mapXML.node[i].@b04y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength4)
						{
							road.road4.push([int(mapXML.node[i].@b05x),int(mapXML.node[i].@b05y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength4)
						{
							road.road4.push([int(mapXML.node[i].@b06x),int(mapXML.node[i].@b06y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength4)
						{
							road.road4.push([int(mapXML.node[i].@b07x),int(mapXML.node[i].@b07y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength4)
						{
							road.road4.push([int(mapXML.node[i].@b08x),int(mapXML.node[i].@b08y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength4)
						{
							road.road4.push([int(mapXML.node[i].@b09x),int(mapXML.node[i].@b09y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength4)
						{
							road.road4.push([int(mapXML.node[i].@b10x),int(mapXML.node[i].@b10y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength4)
						{
							road.road4.push([int(mapXML.node[i].@b11x),int(mapXML.node[i].@b11y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength4)
						{
							road.road4.push([int(mapXML.node[i].@b12x),int(mapXML.node[i].@b12y)]);
							blocknum++;
						}
						if(blocknum < road.roadlength4)
						{
							road.road4.push([int(mapXML.node[i].@b13x),int(mapXML.node[i].@b13y)]);							
							blocknum++;
						}
						if(blocknum < road.roadlength4)
						{
							road.road4.push([int(mapXML.node[i].@b14x),int(mapXML.node[i].@b14y)]);
							blocknum++;
						}						
						mapinfonum++;						
					}
					//if(mapinfonum == 8)
					//{
						
					//}
					
				}
				
			}
			catch(e:TypeError)
			{
				Alert.show("Can't read mapinfo correctly!");
				trace(e.message);
			}

		}
		
		//窗口改变大小
		public function setDisplaySize(width:int,height:int):void
		{
			this.displayWidth = width;
			this.displayHeight = height;
		}
		
		//初始化
		public function init():Boolean
		{			
			//计算地图左上角点
			map.mapIma.xPos = (displayWidth -  map.mapIma.width) / 2;
			map.mapIma.yPos = (displayHeight -  map.mapIma.height) / 2;

			return true;
		}
		
		//地图拖动
		public function dragMap(mouseXMove:int, mouseYMove:int, imageXPos:int, imageYPos:int):Boolean
		{
			//得到鼠标移动的x,y轴分量， 再得到图片左上角点将要移动到点的x,y坐标
			var xTemp:int = imageXPos + mouseXMove;
			var yTemp:int = imageYPos + mouseYMove;
			
			//整张图片完全大于视窗大小
			if(map.mapIma.width >= displayHeight && map.mapIma.width >= displayWidth)
			{					
				if(xTemp > 0)
				{
					map.mapIma.xPos = 0;					
				}
				else if(xTemp + map.mapIma.width < displayWidth)
				{
					map.mapIma.xPos = displayWidth - map.mapIma.width;
				}
				else
				{
					map.mapIma.xPos = xTemp;
				}
				
				if(yTemp > 0)
				{
					map.mapIma.yPos = 0;
				}
				else if(yTemp + map.mapIma.width < displayHeight)
				{
					map.mapIma.yPos = displayHeight - map.mapIma.width;
				}
				else
				{
					map.mapIma.yPos = yTemp;
				}
			}
				
				//仅有上下方超出视窗大小
			else if(map.mapIma.width >= displayHeight && map.mapIma.width <= displayWidth)
			{
				if(yTemp > 0)
				{
					map.mapIma.yPos = 0;
				}
				else if(yTemp + map.mapIma.width < displayHeight)
				{
					map.mapIma.yPos = displayHeight - map.mapIma.width;
				}
				else
				{
					map.mapIma.yPos = yTemp;
				}
			}
				
				//仅有左右超出视窗大小,这种情况很少出现
			else if(map.mapIma.width <= displayHeight && map.mapIma.width >= displayWidth)
			{
				if(xTemp > 0)
				{
					map.mapIma.xPos = 0;
				}
				else if(xTemp + map.mapIma.width < displayWidth)
				{
					map.mapIma.xPos = displayWidth - map.mapIma.width ;
				}
				else
				{
					map.mapIma.xPos = xTemp;
				}
			}
			castle.structure.xPos = map.mapIma.xPos + castle.xPosrelative;
			castle.structure.yPos = map.mapIma.yPos + castle.yPosrelative;
			return true;
		}
		
	}
}